package com.zixuan.sina.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zixuan.sina.dao.PostLikeMapper;
import com.zixuan.sina.entity.PostLike;
import com.zixuan.sina.entity.User;
import com.zixuan.sina.entity.vo.PostVo;
import com.zixuan.sina.utils.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/postlike/")
public class PostLikeController {
    @Resource
    private PostLikeMapper postLikeMapper;

    @GetMapping("thnumLike")
    public Result thnumLike(@RequestParam("postId") String postId){
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal==null){
          return  Result.error();
        }
        String id = principal.getId();
        LambdaQueryWrapper<PostLike> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PostLike::getUserId,id);
        wrapper.eq(PostLike::getPostId,postId);
        PostLike postLike = postLikeMapper.selectOne(wrapper);
        if(postLike==null){
            //点赞
            postLike=new PostLike();
            postLike.setUserId(Long.valueOf(id));
            postLike.setPostId(Long.valueOf(postId));
            postLikeMapper.insert(postLike);
        }else {
            //取消点赞
            postLikeMapper.deleteById(postLike);
        }
        return Result.ok();
    }
}
